import { defineStore } from 'pinia'
import request from '@/utils/request'
import {ApiRes} from '@/views/types/data'
import { Profile, QQUserInfo } from '@/views/types/user'
import { getProfile, removeProfile, setProfile } from '@/utils/storage'

export default defineStore('user',{
  state(){
    return {
      profile:getProfile() as Profile
    }
  },
  actions: {
   async login(data:{account:string,password:string}){
      const res = await request.post<ApiRes<Profile>>('/login',data)
      // console.log(res.data);
      this.profile = res.data.result
      setProfile(res.data.result)
    },
    async sendMobileMsg(mobile:string){
      await request.get('/login/code',{
        params:{
          mobile
        }
      })
      
    },
    async mobileLogin(data: { mobile: string, code: string }) {
      const res = await request.post<ApiRes<Profile>>('/login/code', data)
      // 1. 保存用户信息到 state 中
      this.profile = res.data.result
      setProfile(res.data.result)
    },
    logout() {
      this.profile = {} as Profile
      removeProfile()
    },
    async qqlogin(data: { unionId: string, source: number }){
     const res = await request.post<ApiRes<Profile>>('/login/social',data)
    this.profile = res.data.result
    setProfile(res.data.result)
    },
    async sendQQBindMsg(mobile: string) {
      await request.get('/login/social/code', {
        params: {
          mobile
        }
      })
    }
  }
})

